﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Drawing;

namespace RestFullChatAJAX.Models.DAO
{
    public class AccountDAO
    {
        private static DataTable AccountTB = new DataTable();

        public static Account[] GetAllAccount()
        {
            AccountTB = ConnectHelper.KetnoiCSDL();
            if (AccountTB != null)
            {
                Account[] lstAcc = new Account[AccountTB.Rows.Count];
                int i = 0;
                foreach (DataRow a in AccountTB.Rows)
                {
                    Account acc = new Account(a["Username"].ToString().Trim(),
                            a["Email"].ToString().Trim(), a["Password"].ToString().Trim());
                    if (!String.IsNullOrEmpty(a["Friends"].ToString()))
                        acc.Friend = a["Friends"].ToString();
                    lstAcc[i++] = acc;
                }
                ConnectHelper.DongKetNoi();
                return lstAcc;
            }
            ConnectHelper.DongKetNoi();
            return null;
        }

        public static Account GetAccountByUsername(String username)
        {
            AccountTB = ConnectHelper.KetnoiCSDL();
            if (AccountTB != null)
            {
                foreach (DataRow a in AccountTB.Rows)
                {
                    if (0 == username.Trim().CompareTo(a["Username"].ToString().Trim()))
                    {
                        Account acc = new Account(a["Username"].ToString().Trim(),
                            a["Email"].ToString().Trim(), a["Password"].ToString().Trim());
                        if (!String.IsNullOrEmpty(a["Friends"].ToString()))
                            acc.Friend = a["Friends"].ToString();
                        ConnectHelper.DongKetNoi();
                        return acc;
                    }
                }
                ConnectHelper.DongKetNoi();
            }
            ConnectHelper.DongKetNoi();
            return null;
        }

        public static Account GetAccountByEmail(String email)
        {
            AccountTB = ConnectHelper.KetnoiCSDL();
            if (AccountTB != null)
            {
                foreach (DataRow a in AccountTB.Rows)
                {
                    if (0 == email.Trim().CompareTo(a["Email"].ToString().Trim()))
                    {
                        Account acc = new Account(a["Username"].ToString().Trim(),
                            a["Email"].ToString().Trim(), a["Password"].ToString().Trim());
                        if (!String.IsNullOrEmpty(a["Friends"].ToString()))
                            acc.Friend = a["Friends"].ToString();
                        ConnectHelper.DongKetNoi();
                        return acc;
                    }
                }
                ConnectHelper.DongKetNoi();
            }
            ConnectHelper.DongKetNoi();
            return null;
        }

        public static bool CreateAccount(Account newAccount)
        {
            AccountTB = ConnectHelper.KetnoiCSDL();
            DataRow newRow = AccountTB.NewRow();
            newRow["Username"] = newAccount.Username.Trim();
            newRow["Email"] = newAccount.Email.Trim();
            newRow["Password"] = newAccount.PassWord.Trim();
            try
            {
                AccountTB.Rows.Add(newRow);
                ConnectHelper.Luu(AccountTB);               
                ConnectHelper.DongKetNoi();
            }
            catch (Exception ex)
            {
                ex.ToString();
                ConnectHelper.DongKetNoi();
                return false;
            }
            return true;
        }

        public static bool UpdateAccount(String UserName, Account account)
        {
            AccountTB = ConnectHelper.KetnoiCSDL();
            int index = 0;
            for (index = 0; index < AccountTB.Rows.Count; index++)
                if (0 == UserName.CompareTo(AccountTB.Rows[index]["Username"].ToString().Trim()))
                    break;
            DataRow newRow = AccountTB.Rows[index];
            try
            {
                newRow.BeginEdit();
                newRow["Username"] = account.Username.Trim();
                newRow["Email"] = account.Email.Trim();
                newRow["Password"] = account.PassWord.Trim();
                newRow["Friends"] = account.Friend;
                newRow.EndEdit();
                ConnectHelper.Luu(AccountTB);
                ConnectHelper.DongKetNoi();
            }
            catch (Exception ex)
            {
                ex.ToString();
                ConnectHelper.DongKetNoi();
                return false;
            }
            return true;
        }
    }
}